# -*- coding:utf-8 -*- #

#引入上层目录模块方法
import sys
sys.path.append("..")

from utils.jsondata import json2proxy


"""
storing map pair: {'cmd' : (alarm_start_time, alarm_settings,
latest_alarm_level)}
"""
alarm_list = {}

print "alarm_list dispathcer==",alarm_list

class Dispatcher(object):
    """docstring for Dispatcher"""
    def __init__(self, connection=None):
        print "connection==",connection
        self.conn = connection


    def dispatch(self, data, connection = None):
        """
        first create connection to determin addr
        is connectable for exit thread even though
        alarm is highest level
        """
        '''
        monitor
        data = {'filter': 'monitor_filter', 
        '硬盘剩余': [2.4, "GB"],
        'datetime': 1333011170, 
        'type': 3, 
        'id': 21, 
        ''硬盘使用率': [57.0, "%"], 
        'is_attention': 0}
        
        alarm
        data = {"filter": "alarm_filter", 
        "硬盘剩余": [1.7, "GB"], 
        "begin_time": 1333681971, 
        "level": 1, 
        "type": 3, 
        "id": 201, 
        "硬盘使用率": [71.0, "%"]}

        '''
        print "data dispath",data
        #首先判断是否有数据
        if data == None:
            return
        
        #json_format_data 长度$data  类型 字符串
        json_format_data = json2proxy(data.get_result())
        
        #这边connection一直都是None
        #print "conn===",connection
        if connection:
            self.conn = connection

        #调用core/network.py下的ShortConnection
        #说明：在检查验证服务端是否可怜的同时，传递消息，直接发送
        self.conn.send(json_format_data)
        
        #注释以下内容
        #说明：当连接服务端时候后，不发送消息，服务端复活，也不会发送消息
        
        #is_ok = self.conn.check_is_alive()
        #print "连接是否ok", is_ok
    
        ##msg = '%s' % json.dumps(data.get_result())
        #print "msg======",msg
        #self.conn.send(msg)
        


class MonitorDispatcher(Dispatcher):
    """docstring for MonitorDispatcher"""
    def __init__(self, connection=None):
        # 出入的内容<core.network.ShortConnection object at 0xb7b6c7ec>
        self.conn = connection
        

class AlarmDispatcher(Dispatcher):
    """docstring for AlarmDispatcher"""
    def __init__(self, arg):
        # arg = <core.network.ShortConnection object at 0xb7b6540c>
        super(AlarmDispatcher, self).__init__(arg)


class LoggerDispatcher(Dispatcher):
    """docstring for LoggerDispatcher"""
    def __init__(self, arg):
        super(LoggerDispatcher, self).__init__(arg)
